<?php

namespace App\Http\Requests;

use function foo\func;
use Illuminate\Foundation\Http\FormRequest;

class GoodsRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return auth('admin')->check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title'=>'required',
            'picture'=>'required',
            'price'=>'required',
            'description'=>'required',
//            $attr代表的是被验证的字段名称 $value代表被验证的字段值，$fail自动获取到的错误信息对象
            'category_id'=>function($attr,$value,$fail){
               if(in_array(null,$value)){
                   return $fail('请选择商品分类');
               }
            },
            'content'=>'required',

        ];
    }

    public function messages()
    {
        return [
            'title.required'=>'商品名称不能为空',
            'picture.required'=>'商品图片不能为空',
            'price.required'=>'商品价格不能为空',
             'description.required' =>'商品描述不能为空',
            'content.required' => '商品内容不能为空'
        ];
    }
}
